package com.ideaaedi.log4j2.defender.properties;

import com.ideaaedi.log4j2.defender.defender.DefaultStringMessageDefender;
import com.ideaaedi.log4j2.defender.defender.Log4j2MessageDefender;
import com.ideaaedi.log4j2.defender.strategy.DefenderStrategy;
import org.apache.logging.log4j.core.LogEvent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 脱敏配置项
 *
 * @author JustryDeng
 * @since 2021/7/21 1:15:15
 */
public class DefaultStringProperties {
    
    /**
     * 是否兼容反斜杠
     * <p>
     * 举例说明：
     *     <br/> 开启前: 能识别{"k":"v"}，但不能识别{\"k\":\"v\"}
     *     <br/> 开启后: 能识别{"k":"v"}，也能识别{\"k\":\"v\"}
     * </p>
     */
    private boolean compatBackslash = true;
    
    /** 脱敏的策略 以及 对应的脱敏字段集合 */
    private Map<DefenderStrategy, Set<String>> strategies;
    
    /** key-value 之间的分隔符 */
    private Set<DefaultStringMessageDefender.KeyValueDelimiter> keyValueDelimiter = newSet(
            new DefaultStringMessageDefender.KeyValueDelimiter[]{
                    DefaultStringMessageDefender.KeyValueDelimiter.COLON,
                    DefaultStringMessageDefender.KeyValueDelimiter.EQUAL,
                    DefaultStringMessageDefender.KeyValueDelimiter.ARROW
            }
    );
    
    /**
     * 指定短路插件
     * <p>
     *     当shortCircuitPlugins中存在{@link Log4j2MessageDefender#support(LogEvent)}匹配的插件时，
     *     使用该插件进行脱敏，而不用脱敏器进行脱敏
     * </p>
     */
    private List<Log4j2MessageDefender> shortCircuitPlugins = new ArrayList<>();
    
    /**
     * 创建set
     */
    public  static<T> Set<T> newSet(T[] delimiters) {
        if (delimiters == null) {
            return new HashSet<>();
        }
        return new HashSet<>(Arrays.asList(delimiters));
    }
    
    public boolean isCompatBackslash() {
        return compatBackslash;
    }
    
    public void setCompatBackslash(boolean compatBackslash) {
        this.compatBackslash = compatBackslash;
    }
    
    public Map<DefenderStrategy, Set<String>> getStrategies() {
        return strategies;
    }
    
    public void setStrategies(Map<DefenderStrategy, Set<String>> strategies) {
        this.strategies = strategies;
    }
    
    public List<Log4j2MessageDefender> getShortCircuitPlugins() {
        return shortCircuitPlugins;
    }
    
    public void setShortCircuitPlugins(List<Log4j2MessageDefender> shortCircuitPlugins) {
        this.shortCircuitPlugins = shortCircuitPlugins;
    }
    
    public Set<DefaultStringMessageDefender.KeyValueDelimiter> getKeyValueDelimiter() {
        return keyValueDelimiter;
    }
    
    public void setKeyValueDelimiter(Set<DefaultStringMessageDefender.KeyValueDelimiter> keyValueDelimiter) {
        this.keyValueDelimiter = keyValueDelimiter;
    }
    
    @Override
    public String toString() {
        return "DefaultStringProperties{" +
                "hitStringValueIsJson=" + compatBackslash +
                ", strategies=" + strategies +
                '}';
    }
}